//index.js
//获取应用实例
const app = getApp()
var that
var _points = []
var _polyline = []
var latitude = 30.6411433
var longitude =  103.6879633
var startDate = ""
var endDate = ""
var distance = ""
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    polyline:[],
    
    // polyline: [{
    //   points: _points,
    //   color: "#FF0000DD",
    //   width: 3,
    //   dottedLine: false
    // }],
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    that = this
    // that.setData({
    //   markers: [{
    //     iconPath: "/img/marker_red.png",
    //     id: 0,
    //     latitude: 30.6411433,
    //     longitude: 103.6879633,
    //     width: 50,
    //     height: 50
    //   }]
    // })
    that.setData({
      longitude: 104.0647315982171,
      latitude: 30.656187968222866,
    });
  },
  bindStartDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    startDate=e.detail.value
    this.setData({
      startdate: e.detail.value
    })
  },
  bindEndDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    endDate=e.detail.value
    this.setData({
      enddate: e.detail.value
    })
  },
  searchTrack:function(){
    _points = []
    _polyline = []
    wx.request({
      url: 'https://pi-home.cn/mini-weixin/gps/getGpsList', 
      data: {
        "startDate": startDate,
        "endDate": endDate
      },
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: function (res) {
        var index = 0
        distance = res.data.distance;
        latitude = res.data.gpsList[0].lat
        longitude = res.data.gpsList[0].lon
        for (var i = 0; i < res.data.gpsList.length; i++) {
          if (res.data.gpsList[i].startFlag && index > 0){
            _polyline.push({
              points: _points,
              color: "#1E90FF",
              width: 4,
              dottedLine: false
            })
            console.log(_points)
            _points = [];
          }else{
            index++;
            _points.push({ latitude: res.data.gpsList[i].lat, longitude: res.data.gpsList[i].lon })
          }
          
        }
        
        
        console.log(_points)
        that.setData({
          longitude: longitude,
          latitude: latitude,
          polyline: _polyline,
          distance: distance
        })
        
      }
    })
  }
})

